package stock;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;

public class HttpDownloadFile {

	public static boolean down(String url, String filePath) throws Exception {
		CloseableHttpClient httpClient = HttpClients.createDefault();
		HttpGet post = new HttpGet(url);
		HttpResponse response = httpClient.execute(post);
		HttpEntity entity = response.getEntity();
		InputStream is = entity.getContent();
		File f = new File(filePath);
		if (f.exists()) {
			f.delete();
		}
		f.getParentFile().mkdirs();
		f.createNewFile();
		FileOutputStream fileout = new FileOutputStream(f);
		byte[] buffer = new byte[1024];
		int ch = 0;
		while ((ch = is.read(buffer)) != -1) {
			fileout.write(buffer, 0, ch);
		}
		is.close();
		fileout.flush();
		fileout.close();
		httpClient.close();
		return true;
	}
}
